/*
自己选择的路 ,跪着也要走完。朋友们 , 虽然这个世界日益浮躁起来,只
要能够为了当时纯粹的梦想和感动坚持努力下去 , 不管其它人怎么样,我
们也能够保持自己的本色走下去。
To the world , you will be a person , but to a person , you
will be the world .                               ——AKPower
*/
//作者:AKPower
//算法:
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <queue>
#include <cstdio>
#include <string>
#include <stack>
#include <set>
#define IOS ios::sync_with_stdio(false), cin.tie(0)
using namespace std;
typedef long long ll;
const ll maxn = 2e5 + 100;
const ll INF = 0x3f3f3f3f;
struct node
{
    ll x, y, val;
} e[maxn];
ll f[maxn];
ll n, m;
ll ans_max, sum_max;
bool cmp(node a, node b)
{
    if (a.val == b.val)
    {
        return a.x < b.x;
    }
    return a.val < b.val;
}

ll find(ll x)
{
    if (f[x] == x)
    {
        return x;
    }
    return f[x] = find(f[x]);
}

bool judge(ll id)
{
    // cout << e[id].val << endl;
    for (ll i = 1; i <= n; i++)
    {
        f[i] = i;
    }
    while (id < m && e[id].val == e[id + 1].val)
    {
        id++;
    }
    ll cnt = 0;
    ll sum = 0;
    for (ll i = id; i >= 1; i--)
    {
        if (cnt == n - 1)
            break;
        ll tx = find(e[i].x);
        ll ty = find(e[i].y);
        ll val = e[i].val;
        if (tx == ty)
            continue;
        if (tx > ty)
        {
            f[ty] = tx;
        }
        else
            f[tx] = ty;
        sum += val;
        cnt++;
    }
    if (cnt < n - 1)
        return false;
    if (e[id].val < ans_max)
    {
        ans_max = e[id].val;
        sum_max = sum;
        return true;
    }
    if (e[id].val == ans_max)
    {
        if (sum_max >= sum)
        {
            sum_max = sum;
            return true;
        }
        else
            return true;
    }
    return false;
}

int main()
{
    IOS;
    cin >> n >> m;
    for (ll i = 1; i <= m; i++)
    {
        ll x, y, val;
        cin >> x >> y >> val;
        e[i].x = x;
        e[i].y = y;
        e[i].val = val;
    }
    sort(e + 1, e + 1 + m, cmp);
    ll l = n - 1, r = m;
    ans_max = INF;
    while (l <= r)
    {
        ll id = (l + r) >> 1;
        // cout << id << endl;
        if (judge(id))
        {
            // cout << "j" << endl;
            r = id - 1;
        }
        else
            l = id + 1;
    }
    cout << sum_max << endl;
    getchar();
    getchar();
    return 0;
}
